﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Xml;
using System.Threading;
using OF.Utility.Logging;
using System.Security.Cryptography.X509Certificates;

namespace OF.Utility
{
    /// <summary>
    /// web请求
    /// </summary>
    public class HttpWebRequestService
    {
        /// <summary>
        /// 获取web请求
        /// </summary>
        /// <param name="requestUriString">请求地址</param>
        /// <param name="parmData">参数信息</param>
        /// <param name="methodType">请求类型</param>
        /// <param name="contentType">HTTP头信息</param>
        /// <param name="headers">header</param>
        /// <returns></returns>
        public static string GetRequest(string requestUriString, string parmData, string methodType, string contentType, Dictionary<string, string> headers)
        {
            string htmlContent = "";
            HttpWebRequest request;

            try
            {
                Encoding encoding = Encoding.GetEncoding("UTF-8");
                if (requestUriString.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(requestUriString));
                }
                else
                {
                    request = WebRequest.Create(requestUriString) as HttpWebRequest;
                }

                //如果用户名不为空,则添加身份验证
                request.Credentials = CredentialCache.DefaultCredentials;

                //请求方法
                request.Method = string.IsNullOrEmpty(methodType) ? "POST" : methodType;

                request.CookieContainer = new CookieContainer();

                //请求类型
                request.ContentType = string.IsNullOrEmpty(contentType) ? "application/x-www-form-urlencoded" : contentType;

                //是否允许重定向
                request.AllowAutoRedirect = true;

                if (headers != null)
                {
                    foreach (var kvp in headers)
                    {
                        request.Headers.Add(kvp.Key, kvp.Value);
                    }
                }


                //设置请求字符长度
                byte[] buffer = encoding.GetBytes(parmData);
                request.ContentLength = buffer.Length;

                //将请求信息添加到请求
                if (request.Method.Equals("POST"))
                {
                    Stream reqStream = request.GetRequestStream();
                    reqStream.Write(buffer, 0, buffer.Length);
                    reqStream.Close();
                }

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                //获取请求的返回信息
                Stream resSteam = response.GetResponseStream();
                StreamReader sr = new StreamReader(resSteam);
                htmlContent = sr.ReadToEnd();

                resSteam.Close();
            }
            catch (WebException wex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(wex.Message);
                sb.AppendLine(wex.StackTrace);
                sb.AppendLine(wex.Source);
                Log4netLog.Debug(sb.ToString());
            }
            catch (InvalidOperationException ioex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(ioex.Message);
                sb.AppendLine(ioex.StackTrace);
                sb.AppendLine(ioex.Source);
                Log4netLog.Debug(sb.ToString());
            }
            finally
            {
                request = null;
            }

            return htmlContent;
        }



        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }
}

